package com.fishingwithme.controllers;

import cn.fishingwithme.domain.settings.*;
import com.fishingwithme.domain.settings.*;
import com.fishingwithme.infrastructure.dtos.DTO;
import com.fishingwithme.infrastructure.utils.FileUtils;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping(path = "/common")
@Tag(name = "Common APIs", description = "Common APIs for feedback, agreements, and utility functions")
public class CommonController {


    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private AgrPpService agrPpService;

    @Autowired
    private FileUtils fileUtils;

    @PostMapping("/fb/add")
    @Operation(
            summary = "Create feedback",
            description = "Submit feedback or report an issue"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "Feedback created successfully",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = DTO.class)
                    )
            ),
            @ApiResponse(
                    responseCode = "400",
                    description = "Invalid feedback data"
            )
    })
    public @ResponseBody DTO<Integer> createFeedback(
            @Parameter(description = "Feedback information", required = true)
            @RequestBody Feedback feedback) {
        int result = feedbackService.createFeedback(feedback);
        if (result > 0) {
            return DTO.success(result);
        }
        return DTO.fail(null, "创建反馈失败");
    }


    @GetMapping("/fb/types")
    @Operation(
            summary = "Get feedback types",
            description = "Retrieve list of available feedback types"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "Feedback types retrieved successfully",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = DTO.class)
                    )
            )
    })
    public @ResponseBody DTO<List<ObjectNode>> getFeedbackTypes() {

        List<ObjectNode> types = Arrays.stream(FeedbackType.values())
                .map(FeedbackType::toJson)
                .collect(Collectors.toList());
        return DTO.success(types);
    }

    @GetMapping(path = "/agr")
    @Operation(
            summary = "Get agreement or privacy policy",
            description = "Retrieve the latest agreement or privacy policy by type"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "Agreement retrieved successfully",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = DTO.class)
                    )
            ),
            @ApiResponse(
                    responseCode = "404",
                    description = "Agreement type not found"
            )
    })
    public @ResponseBody DTO<AgrPp> getAgrPp(
            @Parameter(description = "Agreement type (e.g., 'T' for terms, 'P' for privacy)", required = true, example = "T")
            @RequestParam String type) {
        return DTO.success(agrPpService.getLatest(type));
    }

}
